package com.ncp.servlet.product;

import java.io.IOException;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.ncp.model.biz.product.ManageFrontProductBiz;
import com.ncp.model.dao.DB;
import com.ncp.model.entity.product.ProductBasicBean;
import com.ncp.model.entity.product.ProductSortBean;

public class SearchProductServlet extends HttpServlet {

	
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		this.doPost(request, response);
	}

	
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		response.setContentType("text/html");
	
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8"); 
		
			ManageFrontProductBiz mfpb = new ManageFrontProductBiz();
			 DB db = new DB();
			int PageSize=16;
			int PageNow=1;
			String	s_order="";
			List<ProductSortBean> listsort=mfpb.getProductSort();
			String order = "";
			String SortName ="";
			String search = request.getParameter("search");
			String name = request.getParameter("name");
			 db.setSql("select count(*) from PRODUCTBASIC");
			 
			 int PageCount=db.getPageCount(PageSize);
			String s_search = "";
			
			String type = request.getParameter("type");
			 
			if(type.equals("FenYe")){
				String s_PageNow=(String)request.getParameter("PageNow");
				 
				PageNow=Integer.parseInt(s_PageNow);
				if(PageNow>PageCount||PageNow<=0){
					 PageNow=1;
				 }
				order = request.getParameter("order");
				if(order!=""){
				    s_order = "order by "+order;   
				  }
				 if(search!=null&&!search.equals("all")){
					 s_search = "where ProductSortID="+search; 
				 }
				 SortName=request.getParameter("SortName");
                 if(SortName!="")	
                	request.setAttribute("SortName", SortName);
				 if(name.equals("search")){
				 db.setSql("select count(*) from PRODUCTBASIC where ProductName like '%"+search+"%'");
				  PageCount=db.getPageCount(PageSize);
	            	s_search = "where ProductName like '%"+search+"%'"; 
				 }else if(name.equals("sort")){
					 db.setSql("select count(*) from PRODUCTBASIC where ProductSortID="+search+"");
					if(search.equals("all")){
						 
					 SortName ="";
					 request.setAttribute("SortName", SortName);
					 }
					 if(search.equals("all")==false){
					  PageCount=db.getPageCount(PageSize);
		            	s_search = "where ProductSortID="+search; 
					 
				 }
				
			 }
			}
			 if(type.equals("search")){
				
				 db.setSql("select count(*) from PRODUCTBASIC where ProductName like '%"+search+"%'");
				  PageCount=db.getPageCount(PageSize);
	            	s_search = "where ProductName like '%"+search+"%'"; 
			}
			 
			 if(type.equals("ordersort")){
					int ProductSortID = Integer.parseInt(request.getParameter("search"));
				 db.setSql("select count(*) from PRODUCTBASIC where ProductSortID ="+ProductSortID);
				  PageCount=db.getPageCount(PageSize);
	            	s_search = " where ProductSortID ="+ProductSortID+""; 
	            	 order = request.getParameter("way");
					   s_order = "order by "+order; 
					   SortName = request.getParameter("SortName");
					   request.setAttribute("SortName", SortName);
			}
			 if(type.equals("order")){
					
				 order = request.getParameter("way");
				 
				 
				 if(order!=null){
	            	s_order = "order by "+order; 
				 }
			}
			 if(type.equals("sort")){
					
				 
				 if(!search.equals("all")){
				  db.setSql("select count(*) from PRODUCTBASIC where ProductSortID="+search+"");
				 int i_search=Integer.parseInt(search);
				  SortName = mfpb.getProductSortName(i_search);
				
				 request.setAttribute("SortName", SortName);
				 
				  PageCount=db.getPageCount(PageSize);
	            	s_search = "where ProductSortID="+search; 
				 }
				
			}
			 
			
			  
			 
				List<ProductBasicBean> listbasic = mfpb.getProductBasicItem(s_search,s_order,PageNow,PageSize);
				request.setAttribute("order", order);
				request.setAttribute("search", search);
				request.setAttribute("name", name);
				request.setAttribute("PageCount", PageCount+"");
				request.setAttribute("PageNow", PageNow+"");
				request.setAttribute("listbasic", listbasic);
				request.setAttribute("listsort", listsort);
				request.getRequestDispatcher("pages/product/ProductsItem.jsp").forward(request,response);
				
		 }
	}

